﻿//********************************************************************************//
//创建日期：<创建日期，2016-4-13>
//页面创建：<孙雪婷，sunxueting@shanghai3h.com>
//后台逻辑：<张晨斐，zhangchenfei@shanghai3h.com>
//功能说明：地下水信息
//********************************************************************************//
using System;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Entity;
using Web.Class;

namespace Web.Pages.YongShui._6.ZhangWuGL
{
    public partial class DiXiaSXX : BasePage
    {
        #region 属性
        /// <summary>
        /// 地下水信息ID
        /// </summary>
        private int DiXiaSXXID
        {
            get
            {
                return Convert.ToInt32(ViewState["DiXiaSXXID"]);
            }
            set
            {
                ViewState["DiXiaSXXID"] = value;
            }
        }
        /// <summary>
        /// 地下水信息数据集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }
        #endregion
        #region 私有变量
        /// <summary>
        /// 地下水信息BLL
        /// </summary>
        private YS_ZW_DIXIASXXBLL _YS_ZW_DIXIASXXBLL = new YS_ZW_DIXIASXXBLL ();
        /// <summary>
        /// 地下水对象
        /// </summary>
        private YS_ZW_DIXIASXX _YS_ZW_DIXIASXX = new YS_ZW_DIXIASXX();
        /// <summary>
        /// 操作日志BLL
        /// </summary>
        private SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();
        #endregion
        #region 私有方法
        /// <summary>
        /// 获取数据集
        /// </summary>
        private void InitData()
        {
            DataTable dt = _YS_ZW_DIXIASXXBLL.SelectYS_ZW_DIXIASXXesDynamic("I_JLZT = 0", "I_Y DESC, I_M DESC");
            this.dtSource = dt;
            gvDiXiaSXX.DataSource = this.dtSource;
            gvDiXiaSXX.DataBind();
        }
        /// <summary>
        /// 根据当前操作更新各类控件状态
        /// </summary>
        private void InitControl()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    dvOperation.Visible = false;
                    tbOperationBtns.Visible = false;
                    btnTianJia.Enabled = true;
                    lblOperationTitle.Text = "";
                    break;
                case GridViewStatus.Search:
                    dvOperation.Visible = true;
                    tbOperationBtns.Visible = false;
                    btnTianJia.Enabled = true;
                    lblOperationTitle.Text = "查看";
                    break;
                case GridViewStatus.Update:
                    dvOperation.Visible = true;
                    tbOperationBtns.Visible = true;
                    btnTianJia.Enabled = false;
                    lblOperationTitle.Text = "修改";
                    txtKaiZhangN.Enabled = false;
                    txtKaiZhangY.Enabled = false;
                    break;
                case GridViewStatus.Add:
                    dvOperation.Visible = true;
                    tbOperationBtns.Visible = true;
                    btnTianJia.Enabled = false;
                    lblOperationTitle.Text = "新增";
                    txtKaiZhangN.Enabled = true;
                    txtKaiZhangY.Enabled = true;
                    break;
            }
        }
        /// <summary>
        /// 设置添加/编辑数据
        /// </summary>
        private void SetOperationData()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    txtKaiZhangN.Text = "";
                    txtKaiZhangY.Text = "";
                    txtBenYueKZ.Text = "";
                    txtBenYueDZ.Text = "";
                    txtBenYueTK.Text = "";
                    txtDiXiaSBS.Text = "";
                    break;
                case GridViewStatus.Search:
                    this._YS_ZW_DIXIASXX = this._YS_ZW_DIXIASXXBLL.SelectYS_ZW_DIXIASXX(this.DiXiaSXXID);
                    txtKaiZhangN.Text = this._YS_ZW_DIXIASXX.I_Y.ToString();
                    txtKaiZhangY.Text = this._YS_ZW_DIXIASXX.I_M.ToString();
                    txtBenYueKZ.Text = this._YS_ZW_DIXIASXX.N_BENYUEKZ.ToString();
                    txtBenYueDZ.Text = this._YS_ZW_DIXIASXX.N_BENYUEDZ.ToString();
                    txtBenYueTK.Text = this._YS_ZW_DIXIASXX.N_BENYUETP.ToString();
                    txtDiXiaSBS.Text = this._YS_ZW_DIXIASXX.N_DIXIASBS.ToString();
                    break;
                case GridViewStatus.Update:
                    this._YS_ZW_DIXIASXX = this._YS_ZW_DIXIASXXBLL.SelectYS_ZW_DIXIASXX(this.DiXiaSXXID);
                    txtKaiZhangN.Text = this._YS_ZW_DIXIASXX.I_Y.ToString();
                    txtKaiZhangY.Text = this._YS_ZW_DIXIASXX.I_M.ToString();
                    txtBenYueKZ.Text = this._YS_ZW_DIXIASXX.N_BENYUEKZ.ToString();
                    txtBenYueDZ.Text = this._YS_ZW_DIXIASXX.N_BENYUEDZ.ToString();
                    txtBenYueTK.Text = this._YS_ZW_DIXIASXX.N_BENYUETP.ToString();
                    txtDiXiaSBS.Text = this._YS_ZW_DIXIASXX.N_DIXIASBS.ToString();
                    break;
                case GridViewStatus.Add:
                    txtKaiZhangN.Text = "";
                    txtKaiZhangY.Text = "";
                    txtBenYueKZ.Text = "";
                    txtBenYueDZ.Text = "";
                    txtBenYueTK.Text = "";
                    txtDiXiaSBS.Text = "";
                    break;
            }
        }
        /// <summary>
        /// 检查用户输入是否正确
        /// </summary>
        /// <returns></returns>
        private string CheckDataVaild()
        {
            string _ErrMsg = "";
            string _KaiZhangN = this.txtKaiZhangN.Text.Trim();
            //开账年检查
            if (string.IsNullOrEmpty(_KaiZhangN))
            {
                _ErrMsg = "请输入开账年！";
            }
            else if ((_KaiZhangN.Length > 4)||(!PageTools.ValidateString(_KaiZhangN, PageTools.eValidateType.数字)))
            {
                _ErrMsg = "请输入正确的开账年！";
            }
            if (!string.IsNullOrEmpty(_ErrMsg))
            {
                this.txtKaiZhangN.Focus();
                return _ErrMsg;
            }
            //开账月检查
            string _KaiZhangY = this.txtKaiZhangY.Text.Trim();
            if (string.IsNullOrEmpty(_KaiZhangY))
            {
                _ErrMsg = "请输入开账月！";
            }
            else if ((_KaiZhangY.Length > 2) || (!PageTools.ValidateString(_KaiZhangY, PageTools.eValidateType.数字)))
            {
                _ErrMsg = "请输入正确的开账月！";
            }
            else if (Convert.ToInt32(_KaiZhangY) > 12)
            {
                _ErrMsg = "请输入正确的开账月！";
            }
            if (!string.IsNullOrEmpty(_ErrMsg))
            {
                this.txtKaiZhangY.Focus();
                return _ErrMsg;
            }
            //开账金额检查
            string _je = this.txtBenYueKZ.Text.Trim();
            if (string.IsNullOrEmpty(_je))
            {
                _ErrMsg = "请输入本月开账金额！";
            }
            else if ((!PageTools.ValidateString(_je, PageTools.eValidateType.水价)))
            {
                _ErrMsg = "请输入正确的本月开账金额！";
            }
            if (!string.IsNullOrEmpty(_ErrMsg))
            {
                this.txtBenYueKZ.Focus();
                return _ErrMsg;
            }
            //到账金额检查
            _je = this.txtBenYueDZ.Text.Trim();
            if (string.IsNullOrEmpty(_je))
            {
                _ErrMsg = "请输入本月到账金额！";
            }
            else if ((!PageTools.ValidateString(_je, PageTools.eValidateType.水价)))
            {
                _ErrMsg = "请输入正确的本月到账金额！";
            }
            if (!string.IsNullOrEmpty(_ErrMsg))
            {
                this.txtBenYueDZ.Focus();
                return _ErrMsg;
            }       
            //退款金额检查
            _je = this.txtBenYueTK.Text.Trim();
            if (string.IsNullOrEmpty(_je))
            {
                _ErrMsg = "请输入本月退款金额！";
            }
            else if ((!PageTools.ValidateString(_je, PageTools.eValidateType.水价)))
            {
                _ErrMsg = "请输入正确的本月退款金额！";
            }
            if (!string.IsNullOrEmpty(_ErrMsg))
            {
                this.txtBenYueTK.Focus();
                return _ErrMsg;
            }
            //补收金额检查
            _je = this.txtDiXiaSBS.Text.Trim();
            if (string.IsNullOrEmpty(_je))
            {
                _ErrMsg = "请输入地下水补收金额！";
            }
            else if ((!PageTools.ValidateString(_je, PageTools.eValidateType.水价)))
            {
                _ErrMsg = "请输入正确的地下水补收金额！";
            }
            if (!string.IsNullOrEmpty(_ErrMsg))
            {
                this.txtDiXiaSBS.Focus();
                return _ErrMsg;
            }
            string _where = "I_JLZT = 0 AND I_Y = " + _KaiZhangN + " and I_M = " + _KaiZhangY;
            DataTable dt = this._YS_ZW_DIXIASXXBLL.SelectYS_ZW_DIXIASXXesDynamic(_where, "");
            if ((dt != null) && (dt.Rows.Count > 0))
            {
                _ErrMsg = "无法添加重复数据！该开账年月的金额数据已输入，您可以做修改或者新增其他开账年月的数据。";
            }
            return _ErrMsg;
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        private bool SaveDiXiaSXX()
        {
            bool _result = false;
            if (this.Status == GridViewStatus.Update)
            {
                this._YS_ZW_DIXIASXX = this._YS_ZW_DIXIASXXBLL.SelectYS_ZW_DIXIASXX(this.DiXiaSXXID);
            }
            if (this.Status == GridViewStatus.Add)
            {
                this._YS_ZW_DIXIASXX.ID = this.DiXiaSXXID;
                this._YS_ZW_DIXIASXX.I_JLZT = 0;
                this._YS_ZW_DIXIASXX.I_Y = Convert.ToInt32(this.txtKaiZhangN.Text.Trim());
                this._YS_ZW_DIXIASXX.I_M = Convert.ToInt32(this.txtKaiZhangY.Text.Trim());
                this._YS_ZW_DIXIASXX.S_CREATEUSER = LoginInfo.CurrentUser.Account;
                this._YS_ZW_DIXIASXX.D_CREATEDATE = BusinessTool.GetDataBaseDateTime();
            }
            this._YS_ZW_DIXIASXX.N_BENYUEKZ = Convert.ToDouble(this.txtBenYueKZ.Text.Trim());
            this._YS_ZW_DIXIASXX.N_BENYUEDZ = Convert.ToDouble(this.txtBenYueDZ.Text.Trim());
            this._YS_ZW_DIXIASXX.N_BENYUETP = Convert.ToDouble(this.txtBenYueTK.Text.Trim());
            this._YS_ZW_DIXIASXX.N_DIXIASBS = Convert.ToDouble(this.txtDiXiaSBS.Text.Trim());
            this._YS_ZW_DIXIASXX.S_MODIFYUSER = LoginInfo.CurrentUser.Account;
            this._YS_ZW_DIXIASXX.D_MODIFYDATE = BusinessTool.GetDataBaseDateTime();
            OperateState _OperateState = OperateState.Modify;
            if (this.Status == GridViewStatus.Add)
            {
                _OperateState = OperateState.Add;
            }
            this._SYS_LOGBLL.Log<YS_ZW_DIXIASXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "地下水信息", _OperateState, this._YS_ZW_DIXIASXX, null);
            if (this._YS_ZW_DIXIASXXBLL.InsertOrUpdate(this._YS_ZW_DIXIASXX))
            {        
                _result = true;
            }
            return _result;
        }
        /// <summary>
        /// 删除地下水数据
        /// </summary>
        /// <returns></returns>
        private bool DeleteDiXiaSXX()
        {
            bool _result = false;
            this._YS_ZW_DIXIASXX = this._YS_ZW_DIXIASXXBLL.SelectYS_ZW_DIXIASXX(this.DiXiaSXXID);
            this._YS_ZW_DIXIASXX.S_MODIFYUSER = LoginInfo.CurrentUser.Account;
            this._YS_ZW_DIXIASXX.D_MODIFYDATE = BusinessTool.GetDataBaseDateTime();
            this._YS_ZW_DIXIASXX.I_JLZT = -1;
            this._SYS_LOGBLL.Log<YS_ZW_DIXIASXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "地下水信息", OperateState.Delete, this._YS_ZW_DIXIASXX, null);
            if (this._YS_ZW_DIXIASXXBLL.InsertOrUpdate(this._YS_ZW_DIXIASXX))
            {
               
                _result = true;
            }
            return _result;
        }
        #endregion
        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
                InitControl();
            }
        }
        protected void btnBaoCun_Click(object sender, EventArgs e)
        {
            string _ErrMsg = CheckDataVaild();
            if (!string.IsNullOrEmpty(_ErrMsg))
            {
                PageTools.AlertMsg(this, _ErrMsg);
                return;
            }
            if (SaveDiXiaSXX())
            {
                PageTools.AlertMsg(this, "保存成功！");
                InitData();
                this.Status = GridViewStatus.Init;
                InitControl();
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败，请重试！");
            }
        }

        protected void btnFangQi_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;
            InitControl();
            SetOperationData();
        }

        protected void btnTianJia_Click(object sender, EventArgs e)
        {
            this.DiXiaSXXID = 0;
            this.Status = GridViewStatus.Add;
            InitControl();
            SetOperationData();
        }

        protected void btnModify_Command(object sender, CommandEventArgs e)
        {
            if (this.Status == GridViewStatus.Update)
            {
                PageTools.AlertMsg(this, "请先保存或放弃当前修改的地下水信息记录！");
                return;
            }
            else if (this.Status == GridViewStatus.Add)
            {
                PageTools.AlertMsg(this, "请先保存或放弃当前新增的地下水信息记录！");
                return;
            }
            this.DiXiaSXXID = Convert.ToInt32(e.CommandArgument);
            this.Status = GridViewStatus.Update;
            InitControl();
            SetOperationData();
        }

        protected void btnDelete_Command(object sender, CommandEventArgs e)
        {
            int _DiXiaSXXID = Convert.ToInt32(e.CommandArgument);
            if ((this.Status == GridViewStatus.Update) && (_DiXiaSXXID == this.DiXiaSXXID))
            {
                PageTools.AlertMsg(this, "该笔地下水信息正在编辑中，请先放弃修改操作后再进行删除！");
                return;
            }
            //保存当前正在修改的地下水ID信息
            _DiXiaSXXID = this.DiXiaSXXID;
            this.DiXiaSXXID = Convert.ToInt32(e.CommandArgument);
            if (DeleteDiXiaSXX())
            {
                PageTools.AlertMsg(this, "删除成功！");
                InitData();
                this.DiXiaSXXID = _DiXiaSXXID;
            }
            else
            {
                PageTools.AlertMsg(this, "删除失败！请重试！");
            }
        }
        #endregion
    }
}